/*
Copyright 2023 The KubeAdmiral Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package prioritysort

import (
	"context"
	"reflect"
	"testing"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

	fedcorev1a1 "github.com/kubewharf/kubeadmiral/pkg/apis/core/v1alpha1"
	"github.com/kubewharf/kubeadmiral/pkg/controllers/scheduler/framework"
)

func makeCluster(clusterName string) *fedcorev1a1.FederatedCluster {
	return &fedcorev1a1.FederatedCluster{
		ObjectMeta: metav1.ObjectMeta{
			Name: clusterName,
		},
	}
}

func TestPrioritySortSelectClusters(t *testing.T) {
	tests := []struct {
		name             string
		su               *framework.SchedulingUnit
		clusterScoreList framework.ClusterScoreList
		expectedCluster  []string
		expectedResult   *framework.Result
	}{
		{
			name: "2 clusters, no priority, duplicate",
			su: &framework.SchedulingUnit{
				SchedulingMode: fedcorev1a1.SchedulingModeDuplicate,
			},
			clusterScoreList: framework.ClusterScoreList{
				{
					Cluster: makeCluster("foo"),
					Score:   1,
				},
				{
					Cluster: makeCluster("bar"),
					Score:   2,
				},
			},
			expectedCluster: []string{"bar", "foo"},
			expectedResult:  framework.NewResult(framework.Success),
		},
		{
			name: "2 clusters, no priority, binpack",
			su: &framework.SchedulingUnit{
				SchedulingMode:   fedcorev1a1.SchedulingModeDivide,
				ReplicasStrategy: fedcorev1a1.ReplicasStrategyBinpack,
			},
			clusterScoreList: framework.ClusterScoreList{
				{
					Cluster: makeCluster("foo"),
					Score:   1,
				},
				{
					Cluster: makeCluster("bar"),
					Score:   2,
				},
			},
			expectedCluster: []string{"bar", "foo"},
			expectedResult:  framework.NewResult(framework.Success),
		},
		{
			name: "2 clusters, 2 have priority",
			su: &framework.SchedulingUnit{
				SchedulingMode: fedcorev1a1.SchedulingModeDuplicate,
				Priorities: map[string]int64{
					"bar": 50,
					"foo": 100,
				},
			},
			clusterScoreList: framework.ClusterScoreList{
				{
					Cluster: makeCluster("bar"),
					Score:   2,
				},
				{
					Cluster: makeCluster("foo"),
					Score:   1,
				},
			},
			expectedCluster: []string{"foo", "bar"},
			expectedResult:  framework.NewResult(framework.Success),
		},
		{
			name: "2 clusters, 1 has priority",
			su: &framework.SchedulingUnit{
				SchedulingMode: fedcorev1a1.SchedulingModeDuplicate,
				Priorities: map[string]int64{
					"bar": 1,
				},
			},
			clusterScoreList: framework.ClusterScoreList{
				{
					Cluster: makeCluster("bar"),
					Score:   2,
				},
				{
					Cluster: makeCluster("foo"),
					Score:   1,
				},
			},
			expectedCluster: []string{"bar", "foo"},
			expectedResult:  framework.NewResult(framework.Success),
		},
		{
			name: "2 clusters, 1 has priority, not in clusterScoreList",
			su: &framework.SchedulingUnit{
				SchedulingMode: fedcorev1a1.SchedulingModeDuplicate,
				Priorities: map[string]int64{
					"fun": 1,
				},
			},
			clusterScoreList: framework.ClusterScoreList{
				{
					Cluster: makeCluster("bar"),
					Score:   2,
				},
				{
					Cluster: makeCluster("foo"),
					Score:   1,
				},
			},
			expectedCluster: []string{"bar", "foo"},
			expectedResult:  framework.NewResult(framework.Success),
		},
		{
			name: "3 clusters, 1 has priority",
			su: &framework.SchedulingUnit{
				SchedulingMode: fedcorev1a1.SchedulingModeDuplicate,
				Priorities: map[string]int64{
					"foo": 1,
				},
			},
			clusterScoreList: framework.ClusterScoreList{
				{
					Cluster: makeCluster("abc"),
					Score:   3,
				},
				{
					Cluster: makeCluster("bar"),
					Score:   2,
				},
				{
					Cluster: makeCluster("foo"),
					Score:   1,
				},
			},
			expectedCluster: []string{"foo", "abc", "bar"},
			expectedResult:  framework.NewResult(framework.Success),
		},
	}

	p, _ := NewPrioritySort(nil)
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			gotList, result := p.(framework.SelectPlugin).SelectClusters(context.TODO(), test.su, test.clusterScoreList)
			if !reflect.DeepEqual(result, test.expectedResult) {
				t.Errorf("status does not match: %v, want: %v", result, test.expectedResult)
			}
			if len(gotList) != len(test.expectedCluster) {
				t.Errorf("expected select cluster length does not match: %d, want: %d", len(gotList), len(test.expectedCluster))
			}
			for i := range gotList {
				if gotList[i].Cluster.Name != test.expectedCluster[i] {
					t.Errorf("Unexpected select cluster: %s, want: %s", gotList[i].Cluster.Name, test.expectedCluster[i])
				}
			}
		})
	}
}
